/*
 * Created on Oct 22, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package no.ntnu.fp.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;

import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
/**
 * @author tho
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XmlSerializer {
	
	

	public Document toXml(Platform aProject) {
		Element root = new Element("project");
		
		Iterator it = aProject.iterator();
		while (it.hasNext()) {
			Well aPerson = (Well)it.next();
			Element element = personToXml(aPerson);
			root.appendChild(element);
		}
		
		return new Document(root);
	}
	
	public Document toXml(Well aWell)
	{
		Element root = new Element("well");
		
		Element id = new Element("id");
		id.appendChild(String.valueOf(aWell.getId()));	
		root.appendChild(id);
		
		Element crit = new Element("critical");
		
		Element critR = new Element("R");
		critR.appendChild(String.valueOf(aWell.getCritR()));
		crit.appendChild(critR);

		Element critP = new Element("P");
		critP.appendChild(String.valueOf(aWell.getCritPress()));
		crit.appendChild(critP);

		Element critT = new Element("T");
		critT.appendChild(String.valueOf(aWell.getCritTemp()));
		crit.appendChild(critT);
	
		root.appendChild(crit);		
		
		Element limit = new Element("limit");
		
		Element evLimit = new Element("ev");
		evLimit.appendChild(String.valueOf(aWell.getEvLimit()));
		limit.appendChild(evLimit);
		
		Element dtLimit = new Element("dt");
		dtLimit.appendChild(String.valueOf(aWell.getDtLimit()));
		limit.appendChild(dtLimit);
		
		Element pLimit = new Element("p");
		pLimit.appendChild(String.valueOf(aWell.getpLimit()));
		limit.appendChild(pLimit);
		
		Element dPlimit1 = new Element("dp1");
		dPlimit1.appendChild(String.valueOf(aWell.getDpLimit1()));
		limit.appendChild(dPlimit1);
		
		Element dPlimit2 = new Element("dp2");
		dPlimit2.appendChild(String.valueOf(aWell.getdPLimit2()));
		limit.appendChild(dPlimit2);
				
		root.appendChild(limit);
		
		Element data = new Element("data");
		root.appendChild(data);
		
		return new Document(root);
	}

	
	public Document UpdateWellDocument(Document xmlDocument, Well aWell){
		Element root = xmlDocument.getRootElement();
		Element data = null;
		
		for (int i = 0; i < root.getChildElements().size(); i++)
		{
			if(root.getChildElements().get(i).getLocalName().equals("data") ){
				data = root.getChildElements().get(i);
				break;
			}
		}

		Element date = new Element("date");
		
		Element timestamp = new Element("time");
		timestamp.appendChild(String.valueOf(Utilities.now()));
		date.appendChild(timestamp);
		
		Element R0 = new Element("r0");
		R0.appendChild(String.valueOf(aWell.getLastValues()[0]));
		date.appendChild(R0);
		
		Element R1 = new Element("r1");
		R1.appendChild(String.valueOf(aWell.getLastValues()[1]));
		date.appendChild(R1);	
		
		Element pressure = new Element("p");
		pressure.appendChild(String.valueOf(aWell.getLastValues()[2]));
		date.appendChild(pressure);	
		
		Element temperature = new Element("t");
		temperature.appendChild(String.valueOf(aWell.getLastValues()[3]));
		date.appendChild(temperature);
		
		data.appendChild(date);
		
		return xmlDocument;
	}
	
//	public Platform toProject(Document xmlDocument) throws ParseException {
//
//		Platform aProject = new Platform("");
//
//		Element groupElement = xmlDocument.getRootElement();
//		Elements personElements = groupElement.getChildElements("person");
//		
//		for (int i = 0; i < personElements.size(); i++) {
//			Element childElement = personElements.get(i);
//			aProject.addWell(assemblePerson(childElement));
//		}
//		
//		return aProject;
//	}

    public Well toPerson(String xml) throws java.io.IOException, java.text.ParseException, nu.xom.ParsingException {
	nu.xom.Builder parser = new nu.xom.Builder(false);
	nu.xom.Document doc = parser.build(xml, "");
	return assemblePerson(doc.getRootElement());
    }
	
	private Element personToXml(Well aPerson) {
//		DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, java.util.Locale.US);
//		Element element = new Element("person");
//		Element name = new Element("name");
//		name.appendChild(aPerson.getName());
//		Element email = new Element("email");
//		email.appendChild(aPerson.getEmail());
//		Element dateOfBirth = new Element("date-of-birth");
//		dateOfBirth.appendChild(format.format(aPerson.getDateOfBirth()));
//		element.appendChild(name);
//		element.appendChild(email);
//		element.appendChild(dateOfBirth);
//		return element;
		return null;
	}
	
	private Well assemblePerson(Element personElement) throws ParseException {
		String name = null, email = null;
		Date date = null;
		Element element = personElement.getFirstChildElement("name");
		if (element != null) {
			name = element.getValue();
		}
		element = personElement.getFirstChildElement("email");
		if (element != null) {
			email = element.getValue();
		}
		element = personElement.getFirstChildElement("date-of-birth");
		if (element != null) {
			date = parseDate(element.getValue());
		}
	//	return new Well(name, email, date);
		return null;
	}
	
	/**
	 * TODO: handle this one to avoid duplicate code
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	private Date parseDate(String date) throws ParseException {
		DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, java.util.Locale.US);
		return format.parse(date);
	}

	public Well toWell(Document doc)
	{
		Element root = doc.getRootElement();
		
		Elements cat = root.getChildElements();
		
		String id = cat.get(0).getValue();
		Element crit = cat.get(1);
		Element R = crit.getFirstChildElement("R");
		Element P = crit.getFirstChildElement("P");
		Element T = crit.getFirstChildElement("T");
		
		Element limit = cat.get(2);
		Element ev = limit.getFirstChildElement("ev");
		Element dt = limit.getFirstChildElement("dt");
		Element p = limit.getFirstChildElement("p");
		Element dp1 = limit.getFirstChildElement("dp1");
		Element dp2 = limit.getFirstChildElement("dp2");
			
		Well well = new Well(
				Double.parseDouble(T.getValue()), 
				Double.parseDouble(R.getValue()), 
				Double.parseDouble(P.getValue()), 
				Double.parseDouble(ev.getValue()),
				Double.parseDouble(dt.getValue()), 
				Double.parseDouble(dp1.getValue()), 
				Double.parseDouble(dp2.getValue()), 
				Double.parseDouble(p.getValue()),null, false);
		
		Elements data = cat.get(3).getChildElements();
		
		Element dataTime, dataR0, dataR1, dataP, dataT;
		
		for (int i = 0; i < data.size(); i++) 
		{
			dataTime = data.get(i);
			dataR0 = dataTime.getFirstChildElement("r0");
			dataR1 = dataTime.getFirstChildElement("r1");
			dataP = dataTime.getFirstChildElement("p");
			dataT = dataTime.getFirstChildElement("t");
			
			
			double[] wellData = { 
					Double.parseDouble(dataR0.getValue()),
					Double.parseDouble(dataR1.getValue()),
					Double.parseDouble(dataP.getValue()),
					Double.parseDouble(dataT.getValue()),
					
			};
			well.addData(wellData);
		}

		return well;
	}
}

